<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="robot_name" placeholder="请输入机器人名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" @change="getRobotName($event)" />
        </div>
        <div class="head-container" style="border:1px solid #0a328a;">
          <!--机器人列表-->
          <el-table v-loading="false" :data="robotList" @row-click="getSelectdata"
            @selection-change="handleSelectionChange" :cell-style="{ borderColor: 'rgb(1 5 43)' }"
            :header-cell-style="{ borderColor: 'rgb(1 5 43)' }">
            <el-table-column label="机器人编号" align="center" prop="robot_num" />
            <el-table-column label="机器人名称" align="center" prop="robot_name" />
          </el-table>
        </div>
      </el-col>
      <!-- 查询条件区域-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          <el-form-item label="巡检点名称" prop="position_name">
            <el-input v-model="queryParams.position_name" placeholder="请输入巡检点名称" clearable
              @keyup.enter.native="handleQuery" size="small" />
          </el-form-item>
          <el-form-item label="巡检点编码" prop="position_num">
            <el-input v-model="queryParams.position_num" placeholder="请输入巡检点编码" clearable
              @keyup.enter.native="handleQuery" size="small" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!--增删改查按钮区域-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['nssc:patrolpoint:remove']">删除</el-button>
            <span style="color: #3b8ace;font-size: 12px">&nbsp;巡检点=池子编号+标签号</span>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!--  数据列表区域-->
        <el-table v-loading="loading" :data="patrolpointList" @selection-change="handleSelectionChange"
          :cell-style="{ borderColor: 'rgb(1 5 43)' }" :header-cell-style="{ borderColor: 'rgb(1 5 43)' }">
          <!--:span-method="objectSpanMethod4"-->
          <el-table-column type="selection" width="55" align="center" />
          <!--  <el-table-column label="序号" type="index" align="center" prop="id" />-->
          <el-table-column label="机器人名称" align="center" prop="robot_name" />
          <el-table-column label="标签号" align="center" prop="label_num" />
<!--          <el-table-column label="巡检点名称" align="center" prop="position_name" />-->
          <el-table-column label="巡检点标题" align="center" prop="position_num" />
          <!--      <el-table-column label="巡检点编号" align="center" prop="position_num">
        <template slot-scope="scope">
         {{scope.row.num}}#
        </template>
      </el-table-column>-->
         <el-table-column label="距起点距离" align="center" prop="position_dist">
            <template slot-scope="scope">
              {{ scope.row.position_dist }} (m)
            </template>
          </el-table-column>
          <el-table-column label="xyz角度1" align="center">
            <template slot-scope="scope">
              {{ scope.row.x_point1 }},{{ scope.row.y_point1 }},{{ scope.row.z_point1 }}
            </template>
          </el-table-column>
          <el-table-column label="xyz角度2" align="center">
            <template slot-scope="scope">
              {{ scope.row.x_point2}}, {{ scope.row.y_point2}}, {{ scope.row.z_point2}}
            </template>
          </el-table-column>
<!--          <el-table-column label="初始角度" align="center">
            <template slot-scope="scope">
              {{ scope.row.x_point3}}, {{ scope.row.y_point3}}, {{ scope.row.z_point3}}
            </template>
          </el-table-column>-->
<!--          <el-table-column label="变倍" align="center" prop="zoom" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="400">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="bqhUpdate(scope.row)"
                         v-hasPermi="['nssc:patrolpoint:edit']">标签号</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="xjdUpdate(scope.row)"
                         v-hasPermi="['nssc:patrolpoint:edit']">巡检点标题</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['nssc:patrolpoint:edit']">点距/角度/变倍</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['nssc:patrolpoint:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--列表分页区域-->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" style="background:rgb(1 5 43);" />
      </el-col>
    </el-row>
    <!-- 点距/角度设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像头x角度1" prop="x_point1">
              <el-input class="inpSize" v-model="form.x_point1" placeholder="请输入水平角度1(0-360)" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头y角度1" prop="y_point1">
              <el-input v-model="form.y_point1" id="cz1Input" placeholder="请输入垂直角度1(0-90)" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头z角度1" prop="z_point1">
              <el-input v-model="form.z_point1" id="cz1Input" placeholder="请输入z角度1(0-90)" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头x角度2" prop="x_point2">
              <el-input class="inpSize" v-model="form.x_point2" placeholder="请输入x角度2(0-360)" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头y角度2" prop="y_point2">
              <el-input v-model="form.y_point2" id="cz1Input" placeholder="请输入y角度2(0-90)" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头z角度2" prop="z_point2">
              <el-input v-model="form.z_point2" id="cz1Input" placeholder="请输入z角度2(0-90)" style="width: 200px" />
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">
            <el-form-item label="摄像头x初始角度" prop="x_point3">
              <el-input class="inpSize" v-model="form.x_point3" placeholder="请输入x初始角度2(0-360)" style="width: 200px" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">
            <el-form-item label="摄像头y初始角度" prop="y_point3">
              <el-input v-model="form.y_point3" id="cz1Input" placeholder="请输入y初始角度2(0-90)" style="width: 200px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头z初始角度" prop="z_point3">
              <el-input v-model="form.z_point3" id="cz1Input" placeholder="请输入z初始角度2(0-90)" style="width: 200px" />
            </el-form-item>
          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="变倍" prop="zoom">-->
<!--              <el-input v-model="form.zoom" id="cz1Input" placeholder="请输入变倍(1-4)" style="width: 200px" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="距离起点距离" prop="position_dist">
              <el-input v-model="form.position_dist" placeholder="请输入点位到始点距离" style="width: 200px" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 巡检点配置对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="650px" append-to-body>
      <el-form ref="form1" :model="form1" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="巡检点编号" prop="position_num">
              <el-input v-model="form1.position_num"  clearable placeholder="请输入或置空巡检点编号" style="width: 200px" />
              <div style="color:red;font-size: 12px">
                提醒！非巡检点置空编号即可
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 标签号配置对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="350px" append-to-body>
      <el-form ref="form2" :model="form2" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标签号" prop="label_num">
              <el-input v-model="form2.label_num" clearable placeholder="请输入标签号" style="width: 200px" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import "@/assets/styles/robt-bg.css";
import {
  delPatrolPoint,
  getPatrolPoint,
  listPatrolPoint, updatePatrolLabelNum,
  updatePatrolPoint,
  updatePatrolPointNum
} from "@/api/nssc/patrolpoint/patrolpoint";
import {listonPageRobotConfig} from "@/api/nssc/robotconfig/robotconfig";
export default {
  data() {
    return {
      camera_type1:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      robot_name: undefined,
      // 总条数
      total: 0,
      // 巡检点表格数据
      patrolpointList: [],
      //机器人表格数据
      robotList: [],
      // 弹出层标题
      title: "",
      title1: "",
      title2: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      open2: false,
      position_num: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        position_name: undefined,
        position_num: undefined,
        robot_name: undefined,
        position_dist: undefined,
        camera_type:undefined
      },
      //getrobotList请求回来的数据
      myArry:[],
      // 表单参数
      form: {
        q_x_point: 0
      },
      // 表单参数
      form1: {
      },
      form2: {
        label_num:undefined
      },
      // 表单校验
      rules: {
        position_dist: [
          { required: true, message: "距离起点距离不能为空", trigger: "blur" }
        ],
        label_num:[
          { required: true, message: "标签号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();//巡检点列表
    this.getrobotList();//机器人列表
  },
  methods: {
    /** 根据输入框机器人名称，查询机器人列表 */
    getRobotName(robot_name) {
      this.queryParams.robot_name = robot_name;
      this.getrobotList();
    },
    /** 查询机器人列表 */
    getrobotList() {
      this.loading = false;
      listonPageRobotConfig(this.queryParams).then(response => {
        this.robotList = response.rows;
        this.camera_type1 = this.robotList[0].camera_type
      });
    },
    //获取当前选中机器人数据
    getSelectdata(row) {
      this.queryParams.robot_config_id = row.id;
      this.getList();
      this.queryParams.camera_type = row.camera_type
      this.camera_type1 = localStorage.removeItem('camera_type1') //移除页面上一次挂载的
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delPatrolPoint(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 查询巡检点列表 */
    getList() {
      this.loading = true;
      listPatrolPoint(this.queryParams).then(response => {
        this.patrolpointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.reset2();
    },
    // 表单重置
    reset() {
      this.form = {
        position_name: undefined,
        robot_name: undefined
      };
      this.resetForm("form");
    },
    // 表单重置
    reset1() {
      this.form1 = {
        position_num: undefined
      };
      this.resetForm("form1");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        label_num: undefined
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.position_num = null;
      this.queryParams.position_name = null;
      this.queryParams.robot_config_id = null;
      this.queryParams.robot_name = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getPatrolPoint(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "点距/角度设置";
      });
    },
    /** 巡检点配置操作 */
    xjdUpdate(row) {
      const id = row.id || this.ids
      getPatrolPoint(id).then(response => {
        this.form1 = response.data;
        this.open1 = true;
        this.title1 = "巡检点配置";
      });
    },
    /** 标签号配置操作 */
    bqhUpdate(row) {
      const id = row.id || this.ids
      getPatrolPoint(id).then(response => {
        this.form2 = response.data;
        this.open2 = true;
        this.title2 = "标签号配置";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePatrolPoint(this.form).then(response => {
              this.$modal.msgSuccess("设置成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm1: function () {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form1.id != undefined) {
            updatePatrolPointNum(this.form1).then(response => {
              this.$modal.msgSuccess("设置成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm2: function () {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          if (this.form2.id != undefined) {
            updatePatrolLabelNum(this.form2).then(response => {
              this.$modal.msgSuccess("设置成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    //表格相同数据列合并
    flitterData4() {
      let spanOneArr = []
      let concatOne = 0
      this.patrolpointList.forEach((item, index) => {//循环后端查询出来的数据(patrolpointList)
        if (index === 0) {
          spanOneArr.push(1)
        } else {
          //name 修改
          if (item.robot_name === this.patrolpointList[index - 1].robot_name) { //第一列需合并相同内容的字段
            spanOneArr[concatOne] += 1
            spanOneArr.push(0)
          } else {
            spanOneArr.push(1)
            concatOne = index
          }
        }
      })
      return {
        one: spanOneArr,
      }
    }, objectSpanMethod4({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = (this.flitterData4(this.tableData).one)[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col
        }
      }
      //判断是否是第二列，如果是就将第二列相同字段进行合并
      //if(columnIndex === 1) {
      // const _row = (this.flitterData(this.tableData).two)[rowIndex]
      // const _col = _row > 0 ? 1 : 0
      // return {
      // rowspan: _row,
      // colspan: _col
      // }
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-input--medium .el-input__inner {
  font-size: 12px !important;
}
</style>
